<template>
  <div class="bgc-default pad-20">
    <div class="bg-color-white pad-20">
      <div>
        <el-button type="primary" @click="addReason">添加</el-button>
      </div>
      <!--顶部搜索 -->
      <div class="top-operate-default mar-t-20">
        <div class="search-item">
          <label>搜索原因：</label>
          <el-input style="width: 200px;" v-model="searchListQuery.cancelReason" placeholder="请输入原因描述"></el-input>
          <label class="mar-l-20">售后类型：</label>
          <el-select v-model="searchListQuery.reasonTypes" placeholder="类型">
            <el-option label="全部" value=""></el-option>
            <el-option label="取消原因" value="cancelOrderFlag"></el-option>
            <el-option label="退款原因" value="refundMoneyFlag"></el-option>
            <el-option label="退款退货原因" value="refundFlag"></el-option>
          </el-select>
          <el-button-group class="mar-l-20">
            <el-button type="primary" @click="handleSearch">查询</el-button>
            <el-button @click="resetSearch">重置</el-button>
          </el-button-group>
        </div>
      </div>
      <!-- 内容列表 -->
      <div class="table-container mar-t-15">
        <el-table ref="multipleTable" :data="afterSaleReasonList" v-loading="listLoading" element-loading-text="加载中" style="width: 100%"
            header-cell-class-name="default-header" row-class-name="default-row">
          <el-table-column width="240px" label="排序" prop="sortNoIndex"></el-table-column>
          <el-table-column width="400px" label="原因描述" prop="cancelReason"></el-table-column>
          <el-table-column width="400px" label="类型">
            <template slot-scope="scope">
              <template>
                <span v-if="scope.row.cancelOrderFlag == 1">
                  取消原因
                </span>
                <span v-if="scope.row.refundMoneyFlag == 1">
                  退款原因
                </span>
                <span v-if="scope.row.refundFlag == 1">
                  退货退款原因
                </span>
              </template>
            </template>
          </el-table-column>
          <el-table-column min-width="140px" label="操作" align="right" fixed="right">
            <template slot-scope="scope">
              <el-button type="text" size="medium" @click="editReason(scope.row)">编辑</el-button>
              <el-button type="text" size="medium" @click="upReason(scope.row)" v-if="scope.row.minSortNoFlag != 1" >上移</el-button>
              <el-button type="text" size="medium" @click="downReason(scope.row)" v-if="scope.row.maxSortNoFlag != 1">下移</el-button>
              <el-button type="text" size="medium" @click="deleteReason(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 列表分页 -->
      <div class="pagination">
        <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
                       :current-page="searchListQuery.currentPage"
                       :page-sizes="[10, 20, 50, 100]" :page-size="searchListQuery.size"
                       layout="total, sizes, prev, pager, next, jumper"
                       :total="total">
        </el-pagination>
      </div>
    </div>
    <el-dialog title="保存售后原因" :visible.sync="editAfterSaleReasonVisible" :close-on-press-escape="true" width="800px">
      <el-form label-position="right" class="pad-30" :model="afterSaleReason" :rules="rules" ref="afterSaleReason">
        <el-form-item label-width="130px" label="原因描述：" prop="cancelReason">
          <div><el-input v-model="afterSaleReason.cancelReason" placeholder="请输入原因"></el-input></div>
        </el-form-item>
        <el-form-item label-width="130px" label="类型：" prop="reasonTypes">
          <div>
            <el-checkbox v-model="afterSaleReason.cancelOrder">取消原因</el-checkbox>
            <el-checkbox v-model="afterSaleReason.refundMoney">退款原因</el-checkbox>
            <el-checkbox v-model="afterSaleReason.refund">退货退款原因</el-checkbox>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editAfterSaleReasonVisible=false">取消</el-button>
        <el-button type="primary" @click="saveAfterSaleReason">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style lang="less" scoped>
  .select-class {
    margin-right: -5px;
    width: 139px;
  }
</style>

<script>
  import { pageAfterSaleReason, saveAfterSaleReason, deleteAfterSaleReason, upSortNo, downSortNo } from '@/api/after-sale/after-sale-reason'

  export default {
    data() {
      return {
        total: 0,
        listLoading: true,
        searchListQuery: {
          cancelReason: '',
          reasonTypes: ''
        },
        defaultQuery: {
          enterpriseId: this.getEnterpriseId(),
          cancelReason: '',
          reasonTypes: '',
          currentPage: 1,
          size: 10
        },
        reasonTypes: [],
        afterSaleReason: {
          enterpriseId: this.getEnterpriseId(),
          cancelReason: '',
          reasonTypes: [],
          cancelOrder: false,
          refundMoney: false,
          refund: false
        },
        dict: {
          orderStatus: {}
        },
        afterSaleReasonList: [],
        rules: {
          cancelReason: [
            { required: true, message: '售后原因不能为空', trigger: 'blur' }
          ],
          reasonTypes: [
            { required: true, message: '售后类型不能为空', trigger: 'blur' }
          ]
        },
        editAfterSaleReasonVisible: false
      }
    },
    // 初始化页面
    created() {
      this.searchListQuery = { ...this.defaultQuery }
      this.init()
    },
    mounted() {
    },
    methods: {
      // 更改当前页数
      handleCurrentChange(val) {
        this.searchListQuery.currentPage = val
        this.init()
      },
      // 更改当前页面大小
      handleSizeChange(val) {
        this.searchListQuery.size = val
        this.init()
      },
      init() {
        // this.searchListQuery.reasonTypes = ''
        // if (this.reasonTypes) {
        //   this.searchListQuery.reasonTypes = this.reasonTypes.join(',')
        // }
        pageAfterSaleReason(this.searchListQuery).then(response => {
          if (response.isError) {
            return
          }
          this.total = response.data.total
          this.afterSaleReasonList = response.data.records
          Object.assign(this.dict, response.data.dict)
          for (const index in this.afterSaleReasonList) {
            this.afterSaleReasonList[index].sortNoIndex = (this.searchListQuery.currentPage - 1) * 10 + 1 + index * 1
          }
          this.listLoading = false
        })
      },
      addReason() {
        this.afterSaleReason = {
          enterpriseId: this.getEnterpriseId(),
          cancelReason: '',
          reasonTypes: [],
          cancelOrder: false,
          refundMoney: false,
          refund: false
        }
        this.editAfterSaleReasonVisible = true
      },
      editReason(row) {
        Object.assign(this.afterSaleReason, row)
        this.afterSaleReason.cancelOrder = this.afterSaleReason.cancelOrderFlag * 1 === 1
        this.afterSaleReason.refundMoney = this.afterSaleReason.refundMoneyFlag * 1 === 1
        this.afterSaleReason.refund = this.afterSaleReason.refundFlag * 1 === 1
        this.editAfterSaleReasonVisible = true
      },
      upReason(row) {
        upSortNo(row).then(response => {
          if (response.isError) return
          this.init()
        })
      },
      downReason(row) {
        downSortNo(row).then(response => {
          if (response.isError) return
          this.init()
        })
      },
      deleteReason(row) {
        this.$confirm('删除售后原因, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteAfterSaleReason(row.reasonId).then(response => {
            if (response.isError) return
            this.$message({ type: 'success', message: '删除成功！' })
            this.init()
          })
        }).catch(() => {
        })
      },
      saveAfterSaleReason() {
        if (!this.afterSaleReason.cancelOrder && !this.afterSaleReason.refundMoney && !this.afterSaleReason.refund) {
          this.$message.error('类型不能为空!')
          return
        }
        let checkResult = true
        this.$refs.afterSaleReason.validateField(['cancelReason'], (valid) => {
          if (valid) {
            checkResult = false
            return
          }
        })
        if (!checkResult) {
          return
        }
        this.afterSaleReason.cancelOrderFlag = this.afterSaleReason.cancelOrder ? 1 : 0
        this.afterSaleReason.refundMoneyFlag = this.afterSaleReason.refundMoney ? 1 : 0
        this.afterSaleReason.refundFlag = this.afterSaleReason.refund ? 1 : 0
        console.log(this.afterSaleReason)
        saveAfterSaleReason(this.afterSaleReason).then(response => {
          if (response.isError) return
          this.$message({ type: 'success', message: '保存成功！' })
          this.init()
          this.editAfterSaleReasonVisible = false
        })
      },
      handleSearch() {
        this.searchListQuery.currentPage = 1
        this.init()
      },
      resetSearch() {
        this.reasonTypes = []
        this.searchListQuery = { ... this.defaultQuery }
        this.init()
      }
    }
  }

</script>
